import saleService from '@/apis/sale'
import orderService from '@/apis/order'

// pages/sale/sale.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    showModal: false,
    customerName: '',
    goto:'',
    isRecording:false
  },

  showModal(e) {
    const goto=e.currentTarget.dataset.goto
    this.setData({ goto, showModal: true });
    console.log(this.data.goto)
  },

  cancelInput() {
    this.setData({ 
      showModal: false,
      customerName: '',
    });
  },

  confirmInput() {
    const name = this.data.customerName.trim();
    console.log(this.data.goto)
    if (name) {
      // 执行后续逻辑
      this.createOrder(name);
    }else{
      wx.showToast({
        title: '请输入客户名',
        icon: 'none' 
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
    
    saleService.get_sale('接待提示语').then(res => {
     this.setData({
       content: this.formatContent(res.data.content)
     }) 
    })
  },

  formatContent(text) {
    // 将换行符转换为 <br>
    const withBreaks = text.replace(/\n/g, '<br>').replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
    // 解析 Markdown
    return withBreaks;
  },
  createOrder(name){
    //显示加载
    wx.showLoading({
      title: '创建订单中',
      mask: true, 
    })
    if(this.data.goto=='startSale'){
      //开始录音
      const app=getApp()
      app.globalData.isRecording=true
      app.globalData.recorderManager.start({
        duration: 600000,
        sampleRate: 16000,
        numberOfChannels: 1,
        format: 'mp3',
        frameSize: 50
      })
    }
    orderService.create(name).then(res => {
      const app=getApp()
      app.globalData.order=res.data.data
      //隐藏加载
      wx.hideLoading()
      if(this.data.goto=='startSale'){
        wx.switchTab({
          url: '/pages/products/products' 
        })
      }else{
        //跳转到订单详情页
        wx.navigateTo({
          url: '/pages/order/detail/detail'
        })
      }
    })
  },
  uploadRecord(tempFilePath) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp();
    app.checkToken().then(valid => {
      console.log("sale/sale.js onShow() token验证结果: "+valid);
    });

    //先判断是否正在录音
    if(app.globalData.isRecording){
      //弹出提示框
      wx.showModal({
        title: '提示',
        content: '已有工单在录音',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            //跳转到录音页面
            wx.switchTab({
              url: '/pages/products/products'
            })
          }
        }
      })
    }

    this.setData({
      showModal: false,
      customerName: '',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})